<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="dialogShow" @close="dialogClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form v-if="dialogShow" ref="fromData" label-width="120px" :model="fromData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="fromData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="fromData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="fromData.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="fromData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addDepartments, editDepartDetail, getDepartDetail, getDeptsList, getSimpleEmployeeList } from '@/api/departments'
export default {
  name: 'AddDepts',
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    //   检查部门名称是否重复
    const checkNameRepeat = async(rule, val, callback) => {
      // 获取部门列表
      console.log(this.nodeData.id)
      const { depts } = await getDeptsList()
      let isRepeat
      //   找出同部门下是否有同名子部门
      console.log(depts)
      if (this.fromData.id) {
        //   编辑
        isRepeat = depts
          .filter((item) => item.pid === this.nodeData.pid && item.id !== this.nodeData.id)
          .some((item2) => item2.name === val)
      } else {
        //   新增
        isRepeat = depts
          .filter((item) => item.pid === this.nodeData.id)
          .some((item2) => item2.name === val)
      }
      if (isRepeat) {
        callback(new Error('该部门下已存在同名子部门'))
      } else {
        callback()
      }
    }
    // 检查部门编码是否重复
    const checkCodeRepeat = async(rule, val, callback) => {
      // 获取部门列表
      const { depts } = await getDeptsList()
      console.log(depts)
      let isRepeat
      if (this.fromData.id) {
        //   编辑
        isRepeat = depts.filter(item => item.id !== this.fromData.id).some(item => item.code === val)
      } else {
        //   新增
        isRepeat = depts.some(item => item.code === val)
      }
      if (isRepeat) {
        callback(new Error('当前部门编码已存在'))
      } else {
        callback()
      }
    }
    return {
      fromData: {
        code: '', // string	非必须		部门编码，同级部门不可重复
        introduce: '', // string	非必须		介绍
        manager: '', // string	非必须		负责人名称
        name: '' // string	非必须		部门名称
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符'
          }
        ]
      },
      managerList: []
    }
  },
  async created() {
    //   获取负责人
    const res = await getSimpleEmployeeList()
    this.managerList = res
  },
  methods: {
    //   关闭模态框
    dialogClose() {
      // 清除校验
      this.$refs.fromData.resetFields()
      this.fromData = {
        code: '', // string	非必须		部门编码，同级部门不可重复
        introduce: '', // string	非必须		介绍
        manager: '', // string	非必须		负责人名称
        name: '' // string	非必须		部门名称
      }
      this.$emit('update:dialogShow', false)
    },
    // 点击确定
    async btnOk() {
      // 校验
      await this.$refs.fromData.validate()
      //   请求
      if (this.fromData.id) {
        //   编辑
        await editDepartDetail(this.fromData)
        await this.$message.success('修改成功')
      } else {
        //   新增
        await addDepartments({
          ...this.fromData,
          pid: this.nodeData.id
        })
        await this.$message.success('新增成功')
      }

      //   关闭
      this.$emit('update:dialogShow', false)
      // 更新页面数据
      this.$emit('addDepts')
    },
    // 获取当前部门信息
    async getDepartDetail(id) {
      const res = await getDepartDetail(id)
      console.log(res)
      this.fromData = res
    }
  }
}
</script>

<style>
</style>
